package com.crm.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.pojo.Competitor;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.QuotationRecord;
import com.crm.services.CustomerServices;
import com.crm.services.DictionaryServices;
import com.crm.services.QuotationRecordService;
import com.crm.services.SaleTrackingLogServices;
import com.crm.services.SalesOpportunityServices;
import com.crm.services.UserServices;

@Controller
@RequestMapping("/sales")
public class QuotationRecordController {
	
	@Autowired
	private SalesOpportunityServices ss;
	@Autowired
	private DictionaryServices ds;
	@Autowired
	private CustomerServices cs;
	@Autowired
	private UserServices us;
	@Autowired	
	private SaleTrackingLogServices stss;
	@Autowired
	private QuotationRecordService qrs;
	
	/**
	 * 报价记录初始页
	 * @return
	 */
	@RequestMapping("/quotationRecord")
	public ModelAndView findAllquotationRecord() {
		String[] DId=new String[] {"全部数据"};
		JSONArray array=qrs.ListTransJSONArray(0,4,DId);//分页查询所有	
		ModelAndView mv=new ModelAndView("sales/quotationRecord");	
		mv.addObject("salesCount", qrs.queryByCount());
		mv.addObject("salesOpp",array);
		System.out.println("报价记录");	
		return mv;
	}
	/**
	 * 详尽需求的分页查询
	 * @param <E>
	 * @param pageNumberl
	 * @param pageCentent
	 * @return
	 */
	@RequestMapping("/quotationRecordPageable")
	@ResponseBody
	public  JSONObject salePageable(Integer pageNumber,Integer pageCentent, String[] DId) {
		System.out.println("pageNumber:"+pageNumber);
		System.out.println("pageCentent:"+pageCentent);
		System.out.println("DId的长度:"+DId.length);		
		int a=0;
		JSONObject json=new JSONObject();
		for (String string : DId) {
			System.out.println("DId"+a+":"+string);		 
			a+=1;
		}
		json.put("saleOpp",qrs.ListTransJSONArray(pageNumber-1,pageCentent,DId));
		json.put("salesCount", qrs.queryByCount());
		return json;			
	}
	
	/***
	 * 详尽需求新增时的数据字典信息
	 * @return
	 */
	@RequestMapping("/saleAddquotationRecordInfo")
	@ResponseBody
	public JSONArray saleAddquotationRecordInfo() {	
		System.out.println("saleAddquotationRecordInfo");
		JSONArray array=new JSONArray();
		 array.add(ds.ListTransJSONArray(ds.findByDictParentid(1000)));//销售机会类型(1420)
		return array;			
	}
	
	/**
	 *内表，通过多条件+模糊查询对应的负责人名称
	 * @return
	 */
/*	@RequestMapping("/withinsaleAddUnameInfo")
	@ResponseBody
	public JSONArray withinsaleAddUnameInfo(String[] DId) {	
		System.out.println("withinsaleAddUnameInfo");
			return ss.ListTransJSONArray2(ss.queryByAll(0, 5, DId));
	
	}*/
	
	/**
	 *内表，通过多条件+模糊查询对应的机会主题名称
	 * @return
	 */
	@RequestMapping("/withinquotationRecordFindByTheme")
	@ResponseBody
	public JSONArray withinSaleFindByTheme(String[] DId) {	
		System.out.println("withinCompetitorFindByTheme");
			return qrs.ListTransJSONArray3(qrs.queryByAll(0, 5, DId));
	
	}	
	
	/**
	 * 内表,通过多条件+模糊查询对应的客户名称
	 */
	@RequestMapping("/withinquotationRecordCustName")
	@ResponseBody
	public JSONArray saleCustName(String[] DId) {
		System.out.println("内表，触发销售机会对应客户字段的模糊查询:"+DId);
		System.out.println("DId的长度:"+DId.length);	
			return qrs.ListTransJSONArray1(qrs.queryByAll(0, 5, DId));		   		
	}		
	/**
	 * 内表，通过多条件+模糊查询对应的需求主题名字
	 * @param DId
	 * @return
	 */
	@RequestMapping("/withinquotationRecordTheme")
	@ResponseBody
	public JSONArray withinquotationRecordTheme(String[] DId) {
		System.out.println("内表，通过多条件+模糊查询对应的需求主题名字");
		return qrs.ListTransJSONArray4(qrs.queryByAll(0, 5, DId));
		
	}
	
	/**
	 *查找单个销售机会
	 * @return
	 */
	@RequestMapping("/findOnequotationRecord")
	@ResponseBody
	public JSONArray findOneSale(Integer salId) {	
		System.out.println("findOneSale"+salId);
		JSONArray array =new JSONArray();
		array.add(qrs.findBySalId(salId));//查询单个销售机会
		
		return array;			
	}
	/**
	 *  确认需求主题是否重复
	 */
	@RequestMapping("/findByQrTheme")
	@ResponseBody
	public Integer findByDedTheme(String salTheme) {
		System.out.println("触发确认销售机会是否重复:"+salTheme);			

			return qrs.findByQrTheme(salTheme);		
	}
	
	/**
	 *删除单个销售机会
	 * @return
	 */
	@RequestMapping("/quotationRecordDelete")
	@ResponseBody
	public int Delete(Integer salId) {	
		System.out.println("Delete:"+salId);
		qrs.Delete(salId);
		return 1;			
	}
	/**
	 *新增 报价单
	 * @return
	 */
	@RequestMapping("/AddquotationRecord")
	@ResponseBody
	public int Addsale(@ModelAttribute("sale") QuotationRecord sale) {	
		System.out.println("dddddddddddddddddddddddddddddddddddddd");
		return qrs.save(sale);			
	}
	
	/**
	 * 转成订单
	 */
	@RequestMapping("/changeToOrder")
	@ResponseBody
	public Integer changeToOrder(Integer qId) {
		System.out.println("转成订单:"+qId);
		return 	qrs.changeToOrder(qId);	
		
	}
}
